package com.swhysc.information.repository;


import com.swhysc.information.entity.InformationDataColumnEx;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

public interface InformationDataColumnExRepository extends JpaRepository<InformationDataColumnEx, String>, QuerydslPredicateExecutor<InformationDataColumnEx> {
    @Transactional
    int deleteInformationDataColumnsByTableIdIn(Set<String> tableIds);

    @Transactional
    int deleteInformationDataColumnsByTableIdIn(List<String> tableIds);

    @Transactional
    int deleteInformationDataColumnsByIdIn(List<String> ids);

    @Query(value = "SELECT t.EN_NAME as tableName,c.ID,c.EN_NAME,c.CH_NAME,c.COL_TYPE,c.IS_KEY,c.IS_NULL,c.remark,t.ID as tableId,t.DATA_SOURCE_ID " +
            "FROM INFORMATION_DATA_COLUMN_EX c " +
            "INNER JOIN  INFORMATION_DATA_TABLE_EX t " +
            "ON c.TABLE_ID = t.ID " +
            "WHERE TABLE_ID IN ?1", nativeQuery = true)
    List<Object[]> getClomnByTableIds(Set<String> tableIds);


    /**
     * 革命机数据源id查询对应表的字段数据
     * @param dataSourId
     * @return
     */
    @Query(value = "SELECT t.EN_NAME as tableName,c.ID,c.EN_NAME,c.CH_NAME,c.COL_TYPE,c.IS_KEY,c.IS_NULL,c.remark,t.ID as tableId,c.DATA_SOURCE_ID " +
            " FROM INFORMATION_DATA_COLUMN_EX c " +
            " inner JOIN INFORMATION_DATA_TABLE_EX t" +
            " ON c.TABLE_ID = t.ID" +
            " where c.DATA_SOURCE_ID = ?1", nativeQuery = true)
    List<Object[]> getClomnByDataSourIdId(String dataSourId);
}

